/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Servlet;

import Entity.ODoc;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import DataAccess.ODocAccess;
import DataAccess.UserAccess;
import Entity.Account;
import Entity.ODoc;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpSession;

/**
 *
 * @author pdaitoan
 */
public class ODocServlet extends HttpServlet {
   ODocAccess access;
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, ParseException {
        response.setContentType("text/html;charset=UTF-8");
        ODocAccess access = new ODocAccess();
        PersistenceManager pm = PMF.get().getPersistenceManager();
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        PrintWriter out = response.getWriter();
        try {

            String action = request.getParameter("action");

            //DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            Date PubDate = null;
            Date DraftDate = null;
            Date SignDate = null;

            if (action.equals("new")){
                String button = "";
                button = request.getParameter("btnLuu");
                if (button != null)
                {
                    // luu vb
                        //   out.print(button);
                        PubDate = df.parse(request.getParameter("txtPubDate"));
                        DraftDate = df.parse(request.getParameter("txtDraftDate"));
                        SignDate = df.parse(request.getParameter("txtSignDate"));
                        try{
                            ODoc odoc = new ODoc();
                            odoc.setDraftDate(DraftDate);
                            odoc.setDraftUsername(request.getParameter("txtDraftUsername"));
                            odoc.setNotes(request.getParameter("txtNotes"));
                            odoc.setPubDate(PubDate);
                            odoc.setPubNum(request.getParameter("txtPubNum"));
                            odoc.setReceiveByName(request.getParameter("txtReceiveByName"));
                            odoc.setReceivePlaceName(request.getParameter("txtReceivePlaceName"));
                            odoc.setSignByName(request.getParameter("txtSignByName"));
                            odoc.setSignDate(SignDate);
                            odoc.setSubject(request.getParameter("txtSubject"));
                            odoc.setIDocID(request.getParameter("txtIDocID"));
                            odoc.setState(Boolean.FALSE); // Chua Approve
                            odoc.setBriefContent(request.getParameter("txtBriefContent"));
                            odoc.setDocumentName(request.getParameter("fname"));
                            
                            Long id = Long.parseLong(request.getParameter("cbxAccount"));
                            odoc.setAccountID(id);
                            /*
                            PersistenceManager pm2 = PMF.get().getPersistenceManager();
                            Account acc = pm2.getObjectById(Account.class,id);
                            pm2.close();

                            odoc.setUser(acc);
                            */
                           if(access.addODoc(odoc))
                           {
                                out.print("add thanh cong");
                                pm.close();
                                response.sendRedirect("/VanBanDi/DisplayODoc.jsp");
                            }
                        }finally{
                            pm.close();
                        }
                }
                else
                {
                    //tro ve
                    button = request.getParameter("btnTroVe");
                    if(!button.equals(""))
                    {
                        response.sendRedirect("/VanBanDi/VanBanDi.jsp");
                    }
                }                
            }
            else if(action.equals("edit"))
            {   
                String button = "";
                button = request.getParameter("btnSave");
                
                PersistenceManager pm1 = PMF.get().getPersistenceManager();
                Long ODocID ;
 
                ODocID = Long.parseLong(request.getParameter("txtID"));
                ODoc odoc = pm1.getObjectById(ODoc.class,ODocID);
                odoc.setDraftDate(df.parse(request.getParameter("txtDraftDate")));
                odoc.setDraftUsername(request.getParameter("txtDraftUsername"));
                odoc.setNotes(request.getParameter("txtNotes"));
                odoc.setPubDate(df.parse(request.getParameter("txtPubDate")));
                odoc.setPubNum(request.getParameter("txtPubNum"));
                odoc.setReceiveByName(request.getParameter("txtReceiveByName"));
                odoc.setReceivePlaceName(request.getParameter("txtReceivePlaceName"));
                odoc.setSignByName(request.getParameter("txtSignByName"));
                odoc.setSignDate(df.parse(request.getParameter("txtSignDate")));
                odoc.setSubject(request.getParameter("txtSubject"));
                odoc.setIDocID(request.getParameter("txtIDocID"));
                odoc.setBriefContent(request.getParameter("txtBriefContent"));
                odoc.setDocumentName(request.getParameter("fname"));
                if (button != null)
                {
                    odoc.setState(Boolean.FALSE);
                    response.sendRedirect("VanBanDi/DisplayODoc.jsp");
                    pm1.close();
                }
                else if (request.getParameter("btnPublish") != null)
                {
                    odoc.setState(Boolean.TRUE);
                    response.sendRedirect("VanBanDi/DisplayODoc.jsp");
                    pm1.close();
                }

            }
            else if (action.equals("publish"))
            {
                Long ODocID ;

                ODocID = Long.parseLong(request.getParameter("txtODocID"));
                ODoc odoc = pm.getObjectById(ODoc.class,ODocID);
                odoc.setState(Boolean.TRUE);
                response.sendRedirect("VanBanDi/DisplayODoc.jsp");
                pm.close();
            }
            else if(action.equals("search"))
            {
                pm = PMF.get().getPersistenceManager();
                String PubNum, Subject;
                Date FromDate, ToDate;
                PubNum = request.getParameter("txtPubNum");
                Subject = request.getParameter("txtSubject");
                if (request.getParameter("txtFromDate") != "" && request.getParameter("txtToDate") != "")
                {
                    FromDate = df.parse(request.getParameter("txtFromDate"));
                    ToDate = df.parse(request.getParameter("txtToDate"));
                }
                else
                {
                    FromDate = null;
                    ToDate = null;
                }

                List<ODoc> results = access.searchODoc(PubNum, Subject, FromDate, ToDate);
                HttpSession session = request.getSession(true);
                session.setAttribute("odoc", results);
                session.setAttribute("isSearch","y");
                response.sendRedirect("VanBanDen/DisplayODoc.jsp");

                pm.close();
            }
//        } catch (ParseException ex) {
//            out.print(ex);
//            //Logger.getLogger(ODocSevlet.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try
        {
             processRequest(request, response);
        }
        catch(ParseException ex)
        {
            Logger.getLogger(ODocServlet.class.getName()).log(Level.OFF, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try
        {
            processRequest(request, response);
        }
        catch(ParseException ex)
        {
            Logger.getLogger(ODocServlet.class.getName()).log(Level.OFF, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
